package com.example.studymvvmproject01.utils

import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import java.util.*

object RequestUtil {

    fun getRequestBody(hashMap: HashMap<String, String>): RequestBody? {
        val data = StringBuffer()
        if (hashMap != null && hashMap.size > 0) {
            val iter: Iterator<*> = hashMap.entries.iterator()
            while (iter.hasNext()) {
                val entry =
                    iter.next() as Map.Entry<*, *>
                val key = entry.key!!
                val `val` = entry.value!!
                data.append(key).append("=").append(`val`).append("&")
            }
        }
        val jso = data.substring(0, data.length - 1)
        return RequestBody.create("application/x-www-form-urlencoded; charset=utf-8".toMediaTypeOrNull(),
            jso)
    }

}